package util.zxing.qrcodehelper;

import java.util.Hashtable;
import java.util.Map;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Color;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeHelper {
	public static Bitmap write(String msg, int width, int height) throws WriterException{
		Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();  
	    
		// 指定纠错等级  
	    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  
	    // 指定编码格式  	
	    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   
	    
		QRCodeWriter write = new QRCodeWriter();
		BitMatrix bitMatrix = new BitMatrix(width, height);
		
		bitMatrix = write.encode(msg, BarcodeFormat.QR_CODE, width, height, hints);
		Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		
		for(int i = 0; i < width; i++)
			for(int j = 0; j < height; j++)
				if(bitMatrix.get(i, j)){
					bitmap.setPixel(i, j, Color.BLACK);
				}else{
					bitmap.setPixel(i, j, Color.WHITE);
				}
		
		return bitmap;
	}
}
